package View;

import Controller.listeners.Mouse;
import Model.StatusMessage;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.Dimension;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkEvent.EventType;
import javax.swing.event.HyperlinkListener;
import javax.swing.plaf.basic.BasicBorders.FieldBorder;

/**
 * JPanel containing 1 message
 * @author pieterc
 */
public class MessagePanel extends JPanel {

    private StatusMessage sm;
    private JLabel avatar;
    private JEditorPane msg;
    private JLabel time;

    public MessagePanel(StatusMessage sm) {
        this.setBorder(new FieldBorder(Color.white, Color.gray, Color.cyan, Color.ORANGE));
        this.setBackground(Color.white);
        this.sm = sm;
        ImageIcon ii;
        try {
            ii = new ImageIcon(new URL(sm.getUser().getAvatar()));
            avatar = new JLabel(ii);
        } catch (MalformedURLException ex) {
            avatar = new JLabel("no image");
            Logger.getLogger(MessagePanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        avatar.setPreferredSize(new Dimension(60, 60));
        avatar.addMouseListener(new Mouse());
        avatar.setToolTipText(sm.getUser().getRealname());

        msg = new JEditorPane("text/html", "<b>" + sm.getUser().getNickname() + "</b> " + sm.toString());
        this.addMouseListener(new Mouse());
        msg.setEditable(false);
        msg.addMouseListener(new Mouse());
        msg.addHyperlinkListener(new HyperlinkListener() {

            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType().equals(EventType.ACTIVATED)) {
                    java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
                    if (Desktop.isDesktopSupported()) {
                        try {
                            desktop.browse(e.getURL().toURI());
                        } catch (IOException ex) {
                            Logger.getLogger(MessagePanelContainer.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (URISyntaxException ex) {
                            Logger.getLogger(MessagePanelContainer.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
        });

        msg.setToolTipText("Posted with " + "todo" + " from " + sm.getUser().getProtocolName());

        int timeAgo = Math.round((new Date().getTime() - sm.getTime().getTime()) / 1000);

        JLabel timeLabel = new JLabel(timeAgo + "s ago");
        if (timeAgo / 60 > 0 && timeAgo > 0) {
            timeAgo /= 60;
            timeLabel.setText(timeAgo + "min ago");
            if (timeAgo / 60 > 0 && timeAgo > 0) {
                timeAgo /= 60;
                timeLabel.setText(timeAgo + "hours ago");
                if (timeAgo / 24 > 0 && timeAgo > 0) {
                    timeAgo /= 24;
                    timeLabel.setText(timeAgo + "days ago");
                }
            }
        }

        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        this.add(avatar);
        this.add(msg);
        this.add(timeLabel);
        this.setPreferredSize(new Dimension(400, 100));

        timeLabel.setToolTipText(sm.getTime().toString());

        setBGColor();
    }

    public StatusMessage getSm() {
        return sm;
    }

    private void setBGColor() {
        msg.setBackground(Color.getHSBColor(20, 20, 20));
        this.setBackground(Color.getHSBColor(20, 20, 20));
//        if (sm.getText().contains(p.getProperty("username"))) {
//            msg.setBackground(Color.red);
//            this.setBackground(Color.red);
//        }
//        if (sm.getUser().getNickname().contains(p.getProperty("username"))) {
//            msg.setBackground(Color.getHSBColor(60, (float) 0.4, (float) 0.7));
//            this.setBackground(Color.getHSBColor(60, (float) 0.4, (float) 0.7));
//        }

    }
}
